package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsParamConstant;
import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.facade.request.BankFourElementVerifyRequest;
import cc.rengu.igas.smps.facade.response.BankFourElementVerifyResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * 银行卡四要素验证服务
 * Created by wangcheng on 2021/4/8 9:47
 */
public class BankFourElementVerifyService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        BankFourElementVerifyRequest bankFourElementVerifyRequest = new BankFourElementVerifyRequest();
        BankFourElementVerifyResponse bankFourElementVerifyResponse = new BankFourElementVerifyResponse();
        try {
            ConvertUtil.convertOutput(bankFourElementVerifyRequest);
            bankFourElementVerifyResponse.setHeader(bankFourElementVerifyRequest.getHeader());
            String acctNo = bankFourElementVerifyRequest.getAcctNo();
            String certType = bankFourElementVerifyRequest.getCertType();
            String certNo = bankFourElementVerifyRequest.getCertNo();
            String mobileNo = bankFourElementVerifyRequest.getMobileNo();
            String customerName = bankFourElementVerifyRequest.getCustomerName();
            if (StringUtil.isNullorEmpty(acctNo) || StringUtil.isNullorEmpty(certType) || StringUtil.isNullorEmpty(certNo) ||
                    StringUtil.isNullorEmpty(mobileNo) || StringUtil.isNullorEmpty(customerName)) {
                rglog.error("缺少一个或多个业务参数！");
                bankFourElementVerifyResponse.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
                bankFourElementVerifyResponse.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            } else {
                /* 演示环境默认返回成功 */
                if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
                    bankFourElementVerifyResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                    bankFourElementVerifyResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                } else {
                    int iReturnCode = schedule.callService(SmpsServiceConstant.BANK_CORE_COMMUNICATION_CLIENT, SmpsParamConstant.DEFAULT_SERVICE_LABEL);
                    if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                        bankFourElementVerifyResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                        bankFourElementVerifyResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                        //TODO 根据具体项目通道返回结果赋值
//                        bankFourElementVerifyResponse.setCertType("");
//                        bankFourElementVerifyResponse.setCertNo("");
//                        bankFourElementVerifyResponse.setMobileNo("");
//                        bankFourElementVerifyResponse.setAcctNo("");
                    } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.BANK_CORE_COMMUNICATION_CLIENT, iReturnCode);
                        bankFourElementVerifyResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                        bankFourElementVerifyResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                    } else {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.BANK_CORE_COMMUNICATION_CLIENT, iReturnCode);
                        bankFourElementVerifyResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                        bankFourElementVerifyResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                    }
                }
            }
            ConvertUtil.convertInput(bankFourElementVerifyResponse);
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            bankFourElementVerifyResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bankFourElementVerifyResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
